/**
 * fileName: DaoActivityImpl.java
 * fileTime: 2011-6-14上午11:07:41
 */
package com.bf.xpdd.persistent.sqlmapdao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.bf.xpdd.domain.ActivityBean;
import com.bf.xpdd.domain.SiteSeller;
import com.bf.xpdd.persistent.iface.DaoActivity;
import com.ibatis.sqlmap.client.SqlMapClient;

/**
 * 活动Dao对应的实现类 需要在spring中进行注册
 * 
 * @author linmu
 * 
 */
public class DaoActivityImpl implements DaoActivity
{

	private Logger logger = Logger.getLogger(DaoActivityImpl.class);

	/**
	 * ibatis的查询接口
	 */
	private SqlMapClient sqlMapClient;

	/**
	 * 注入sqlMapClient
	 * 
	 * @param sqlMapClient
	 */
	public void setSqlMapClient(SqlMapClient sqlMapClient)
	{
		this.sqlMapClient = sqlMapClient;
	}

	@Override
	public void addActivity(ActivityBean bean)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.insert("activity.addActivityBean", bean);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void delActivity(ActivityBean bean)
	{
		delActivity(bean.getAid());
	}

	@Override
	public void delActivity(long aid)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.delete("activity.delActivityByBean", aid);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void modifyActivity(ActivityBean bean)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.update("activity.modifyActivityBean", bean);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void modifyActivityStatus(int status, long aid, String nick)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("status", status);
		map.put("aid", aid);
		map.put("nick", nick);
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.update("activity.modifyActivityStatus", map);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void addLoginSize(int size, long aid, String nick)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("size", size);
		map.put("aid", aid);
		map.put("nick", nick);
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.update("activity.addLoginSize", map);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void addDealNum(int size, long aid, String nick)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("size", size);
		map.put("aid", aid);
		map.put("nick", nick);
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.update("activity.addDealNum", map);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void modifyActivityScope(int scope, long aid, String nick)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("scope", scope);
		map.put("aid", aid);
		map.put("nick", nick);
		try
		{
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.update("activity.modifyActivityScope", map);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void modifyActivityHelp(boolean isHelp, long aid, String nick)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("aid", aid);
		map.put("nick", nick);
		try
		{
			int param = isHelp ? 1 : 0;
			map.put("param", param);
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.update("activity.modifyActivityHelp", param);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void modifyActivityEave(boolean isEave, long aid, String nick)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("aid", aid);
		map.put("nick", nick);
		try
		{
			int param = isEave ? 1 : 0;
			map.put("param", param);
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.update("activity.modifyActivityEave", param);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void modifyActivityFavorsite(boolean isFavorsite, long aid, String nick)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("aid", aid);
		map.put("nick", nick);
		try
		{
			int param = isFavorsite ? 1 : 0;
			map.put("param", param);
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.update("activity.modifyActivityFavorsite", param);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public void modifyActivityShare(boolean isShare, long aid, String nick)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("aid", aid);
		map.put("nick", nick);
		try
		{
			int param = isShare ? 1 : 0;
			map.put("param", param);
			this.sqlMapClient.startTransaction();
			this.sqlMapClient.update("activity.modifyActivityShare", map);
			this.sqlMapClient.commitTransaction();
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
	}

	@Override
	public List<ActivityBean> queryAllActivities()
	{
		try
		{
			this.sqlMapClient.startTransaction();
			List<ActivityBean> list = (List<ActivityBean>) this.sqlMapClient
					.queryForList("activity.queryAllActivities");
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public ActivityBean queryActivityInfoById(long aid)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			List<ActivityBean> list = (List<ActivityBean>) this.sqlMapClient.queryForList(
					"activity.queryActivityInfoById", aid);
			this.sqlMapClient.commitTransaction();
			if (list.size() > 0)
			{
				return list.get(0);
			}
			return null;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public List<ActivityBean> queryActivityInfoByNick(String nick)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			List<ActivityBean> list = (List<ActivityBean>) this.sqlMapClient.queryForList(
					"activity.queryActivityInfoByNick", nick);
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public List<ActivityBean> queryActivitiesByPage(int pageNum, int pageSize)
	{
		int startIndex = (pageNum - 1) * pageSize;

		try
		{
			this.sqlMapClient.startTransaction();
			Map<String, Integer> map = new HashMap<String, Integer>();
			map.put("startIndex", startIndex);
			map.put("size", pageSize);
			List<ActivityBean> list = (List<ActivityBean>) this.sqlMapClient.queryForList(
					"activity.queryActivitiesByPage", map);
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public List<ActivityBean> queryActivitiesByMonth(SiteSeller seller, int month) throws SQLException
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("month", month);
		map.put("nick", seller.getNick());
		return (List<ActivityBean>) this.sqlMapClient.queryForList("activity.queryActivityInfoByMonth", map);
	}

	@Override
	public List<ActivityBean> queryActivitiesByStatus(String nick, int status)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("nick", nick);
		map.put("status", Integer.valueOf(status));
		try
		{
			this.sqlMapClient.startTransaction();
			List<ActivityBean> list = (List<ActivityBean>) this.sqlMapClient
					.queryForList("activity.queryActivitiesByStatus", map);
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public List<Long> queryAllModelIds(String nick)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			List<Long> list = (List<Long>) this.sqlMapClient.queryForList("activity.queryAllModelIds", nick);
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public List<ActivityBean> queryActivityByModel(long modelId)
	{
		try
		{
			this.sqlMapClient.startTransaction();
			List<ActivityBean> list = (List<ActivityBean>) this.sqlMapClient
					.queryForList("activity.queryActivityByModel", modelId);
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}

	public List<ActivityBean> queryUnDeadActivities()
	{
		try
		{
			this.sqlMapClient.startTransaction();
			List<ActivityBean> list = (List<ActivityBean>) this.sqlMapClient
					.queryForList("activity.queryUnDeadActivities");
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return new ArrayList<ActivityBean>(0);
	}

	@Override
	public List<ActivityBean> queryActivityLimit(long aid, String nick, int size)
	{
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("aid", aid);
		map.put("nick", nick);
		map.put("size", size);
		try
		{
			this.sqlMapClient.startTransaction();
			List<ActivityBean> list = (List<ActivityBean>) this.sqlMapClient
					.queryForList("activity.queryActivityLimit", map);
			this.sqlMapClient.commitTransaction();
			return list;
		}
		catch (SQLException e)
		{
			this.logger.error(e.toString());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				this.sqlMapClient.endTransaction();
			}
			catch (SQLException e)
			{
				this.logger.error(e.toString());
				e.printStackTrace();
			}
		}
		return null;
	}
}
